home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.11 Nov 94 / Threads / TestThreads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-07  |  2.2 KB  |  93 lines  |  [TEXT/MPS ]

  1. #include <threads.h>
  2. #include <stdio.h>
  3. #include <GestaltEqu.h>
  4.  
  5. #if defined(powerc) || defined (__powerc)
  6. #include <FragLoad.h>
  7. #endif
  8.  
  9. pascal void *MyThread_A( void *refCon);
  10. pascal void *MyThread_B( void *refCon);
  11. pascal void *MyThread_C( void *refCon);
  12.  
  13. void    main( void)
  14. {
  15.     OSErr        errWhatErr;
  16.     int            i;
  17.     ThreadID    threadID_A, threadID_B, threadID_C;
  18.     long        threadGestaltInfo;
  19.         
  20. //    Test for the presence of the threads manager
  21. //    1. Is the gestalt selector defined?  If not, we bail immediately
  22. //    2. If we're compiling PPC native code, then check for the ThreadsLibrary
  23. //    3. Also, if we're native, check that CFM actually linked my app to the library
  24. //    4. Is the Thread Mgr Present bit set to True?  If not, bail
  25.  
  26.     if( Gestalt( gestaltThreadMgrAttr, &threadGestaltInfo) != noErr ||
  27. #if defined(powerc) || defined (__powerc)
  28.         threadGestaltInfo & (1<<gestaltThreadsLibraryPresent) == 0 ||
  29.         (Ptr) NewThread == kUnresolvedSymbolAddress ||
  30. #endif
  31.         threadGestaltInfo & (1<<gestaltThreadMgrPresent) == 0)
  32.     {
  33. //    This is the bail clause.  Yours may be more elaborate
  34.         printf( "The Threads Mgr isn't present... Can't run the test.\n");
  35.         return;
  36.     }
  37.     
  38. //    Create 3 threads: A, B, C
  39.     errWhatErr = NewThread( kCooperativeThread, MyThread_A, (void *)0, 0,
  40.                             kFPUNotNeeded + kCreateIfNeeded,
  41.                             (void**)nil, &threadID_A);
  42.  
  43.     errWhatErr = NewThread( kCooperativeThread, MyThread_B, (void *)0, 0,
  44.                             kFPUNotNeeded + kCreateIfNeeded,
  45.                             (void**)nil, &threadID_B);
  46.  
  47.     errWhatErr = NewThread( kCooperativeThread, MyThread_C, (void *)0, 0,
  48.                             kFPUNotNeeded + kCreateIfNeeded,
  49.                             (void**)nil, &threadID_C);
  50.  
  51. //    Simple loop to test Yielding
  52.     for( i = 0; i < 5; i++) {
  53.         printf( "This is thread main\n");
  54.         YieldToAnyThread();
  55.     }
  56. }
  57.  
  58. pascal void *MyThread_A( void * /* refCon */)
  59. {
  60.     int            i;
  61.     
  62.     for( i = 0; i < 5; i++) {
  63.         printf( "------- A ------\n");
  64.         YieldToAnyThread();
  65.     }
  66.     
  67.     return nil;
  68. }
  69.  
  70. pascal void *MyThread_B( void * /* refCon */)
  71. {
  72.     int            i;
  73.     
  74.     for( i = 0; i < 5; i++) {
  75.         printf( "------- B ------\n");
  76.         YieldToAnyThread();
  77.     }
  78.  
  79.     return nil;
  80. }
  81.  
  82. pascal void *MyThread_C( void * /* refCon */)
  83. {
  84.     int            i;
  85.     
  86.     for( i = 0; i < 5; i++) {
  87.         printf( "------- C ------\n");
  88.         YieldToAnyThread();
  89.     }
  90.  
  91.     return nil;
  92. }
  93.